package com.ztom.v2;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author ZhangTao
 */
public class Code72DailyTemperatures {

    public int[] dailyTemperatures(int[] temperatures) {
        if (temperatures == null || temperatures.length == 0) {
            return null;
        }

        int[] res = new int[temperatures.length];
        Deque<Integer> stack = new LinkedList<>();
        for (int i = 0; i < temperatures.length; i++) {
            while (!stack.isEmpty() && temperatures[stack.peekLast()] < temperatures[i]) {
                Integer pre = stack.pollLast();
                res[pre] = i - pre;
            }
            stack.addLast(i);
        }
//        while (!stack.isEmpty()) {
//            Integer pre = stack.pollLast();
//            res[pre] = 0;
//        }
        return res;
    }
}
